package com.itheima.service;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.domain.UserDetail;
import com.itheima.vo.UserDetailVo;
import com.itheima.vo.UserInfoVo;
import com.itheima.vo.VisitorListVo;
import com.itheima.vo.VisitorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public UserInfoVo getUserInfo(String userId) {
        // 查询当前用户的信息
        User user = userDao.findById(userId);
        if (Objects.isNull(user)) {
            return null;
        }

        // 查询我的粉丝数
        int fansCount = userDao.getFansCount(userId);
        // 查询我喜欢的用户数
        int likeCount = userDao.getLikeCount(userId);
        // 查询互相关注数
        int followCount = userDao.getFollowCount(userId);

        UserInfoVo vo = new UserInfoVo();
        vo.setAge(user.getAge());
        vo.setAvatar(user.getAvatar());
        vo.setGender(user.getGender());
        vo.setRegion(user.getRegion());
        vo.setUserName(user.getUserName());
        vo.setLikeAmount(likeCount);
        vo.setFollowAmount(followCount);
        vo.setFansAmount(fansCount);
        return vo;
    }

    @Override
    public UserDetailVo getUserDetail(String userId) {
        User user = userDao.findById(userId);
        UserDetail userDetail = userDao.findDetailById(userId);

        UserDetailVo vo = new UserDetailVo();
        if(Objects.nonNull(user)) {
            vo.setId(user.getId());
            vo.setAge(user.getAge());
            vo.setAvatar(user.getAvatar());
            vo.setGender(user.getGender());
            vo.setUserName(user.getUserName());
            vo.setRegion(user.getRegion());
        }

        if(Objects.nonNull(userDetail)) {
            vo.setEducation(userDetail.getEducation());
            vo.setIncome(userDetail.getIncome());
            vo.setLevel(userDetail.getLevel());
            vo.setMarry(userDetail.getMarry());
            vo.setIndustry(userDetail.getIndustry());
        }

        return vo;
    }

    @Override
    public Boolean updateById(User user) {
        return userDao.updateById(user) > 0;
    }

    @Override
    public VisitorVo getVisitorList(String userId) {
        int visitCount = userDao.getVisitCount(userId);
        List<VisitorListVo> visitList = userDao.getVisitList(userId);

        VisitorVo vo = new VisitorVo();
        vo.setTotal(visitCount);
        vo.setList(visitList);

        return vo;
    }
}
